
#include "tools/socket.h"
#include "tools/select.h"
#include "log.h"
#include <stdio.h>


static tools::CSocket   s;
static tools::CSocket   w;
static tools::CSocket   r(tools::CSocket::eSocketType_Console);
static tools::CSelect   selectA;

int __read_callback(tools::CSocket* sock)
{
    if (sock == &s) {
        s.Accept(w);
        selectA.AddReadSocket(&w);
    } else if (sock == &w) {
        char    buffer[4096];
        int ret = sock->Recv(buffer, sizeof(buffer) - 1);
        buffer[ret] = '\0';
        printf("%s", buffer);
        fflush(stdout);
    } else if (sock == &r) {
        char    buffer[4096];
        int ret = sock->Recv(buffer, sizeof(buffer) - 1);
        w.Send(buffer, ret);
    } else {
    }
    return 0;
}


int main()
{
    logging::DecorateDefaultLogTracer();
    try {
        s.Create();
        r.Create();
        s.Bind(10234);
        s.Listen();
        selectA.AddReadSocket(&s);
        selectA.AddReadSocket(&r);
        selectA.RegisterReadCallback(__read_callback);

        selectA.Run();
    } catch (const char * e) {
        printf("%s\n", e);
    }

    return 0;
}

